package com.project.quiktask.controller;
/**
 * This class is in charge of handling the add command
 * @author PHAM MINH VIET
 */

import java.io.IOException;
import java.text.ParseException;
import java.util.Vector;

import com.project.quiktask.Task;
import com.project.quiktask.datastore.FileStorer;
import com.project.quiktask.exception.StartDateAfterEndDateException;
import com.project.quiktask.exception.StartTimeAfterEndTimeException;
import com.project.quiktask.parser.Parser;
import com.project.quiktask.util.Result;
import com.project.quiktask.util.Variable;


public class AddHandler implements CommandHandler{
	Task newTask;

	public AddHandler() {

	}

	public Result execute(String parameter) {
		try {
			String[] fields=Parser.parseString(parameter);
			newTask = TaskParser.getTaskFromString(fields);
			if(newTask.getTaskDescription()==null){
				return new Result(Variable.NO_DESCRIPTION_TASK);
			}
			FileStorer storer = new FileStorer();
			storer.storeTask(newTask);
		} catch (ParseException e1) {
			return new Result("Input error!");
		} catch (IOException e) {
			return new Result("File storer error");
		} catch (StartDateAfterEndDateException e) {
			return new Result(e.getMessage());
		} catch (StartTimeAfterEndTimeException e) {
			return new Result(e.getMessage());
		}
		Vector<Task> tasksToBeDisplayed=new Vector<Task>();
		tasksToBeDisplayed.add(newTask);
		return new Result(Variable.FEEDBACK_ADD_SUCCESSFUL, tasksToBeDisplayed);
	}

	public static Result addOneTask(Task newTask) throws IOException	{
		try {
			FileStorer storer = new FileStorer();
			storer.storeTask(newTask);
		} catch (IOException e) {
			Result result = new Result("File storer error");
			return result;
		}
		Vector<Task> tasksToBeDisplayed=new Vector<Task>();
		tasksToBeDisplayed.add(newTask);
		Result result = new Result("Hello world", tasksToBeDisplayed);
		return result;
	}

	public Result undo(){
		//delete tasks
		try {
			DeleteHandler.deleteOneTask(newTask);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return new Result("Task: "+newTask.getTaskDescription()+" was deleted successfully");
	}

}
